package cn.llssit.project.wechatapi.mapper;

import cn.llssit.project.wechatapi.domain.Orders_Detail;
import cn.llssit.project.wechatapi.domain.Orders_Person_Detail;
import cn.llssit.project.wechatapi.domain.Seat;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author: Lesan
 * @Date: 2020-06-12
 */
public interface OrdersMapper {

    @Insert("INSERT INTO orders VALUES(null,#{Mid},#{Vid},#{clmn},#{rws},#{Rid},#{price},#{time},#{Fid})")
    void addOrder(Integer Mid, Integer Vid, Integer clmn, Integer rws, Double price, String time, Integer Fid, Integer Rid);

    @Select("SELECT clmn,rws FROM orders WHERE Mid = #{Mid}")
    List<Seat> boughtTicket(Integer Mid);

    @Select("SELECT any_value(orders.id) id,any_value(Vid) Vid,any_value(clmn) clmn,any_value(rws) rws,time,COUNT(*) num,any_value(price*COUNT(*)) AS price,any_value(`name`) `name`,any_value(poster) poster,any_value((SELECT startDate FROM movie_release WHERE orders.Mid = movie_release.id)) Mdate,any_value((SELECT endDate FROM movie_release WHERE orders.Mid = movie_release.id)) Edate FROM orders,movie WHERE Fid=movie.id AND Rid=#{Rid} GROUP BY time")
    List<Orders_Person_Detail> myOrders(Integer Rid);

    @Select("<script>" +
            "SELECT any_value(orders.id) id,any_value(orders.Vid) Vid,any_value(clmn) clmn,any_value(rws) rws,time,COUNT(*) num,any_value(orders.price*COUNT(*)) AS price,any_value(`name`) `name`,any_value(poster) poster,any_value(movie_release.startDate) Mdate,any_value(movie_release.endDate) Edate,any_value(phone) phone FROM orders,movie,movie_release,customer WHERE Fid=movie.id AND movie_release.id=orders.Mid AND orders.Rid = customer.id " +
            "<if test='startDate!=null'>" +
            "AND startDate = #{startDate} " +
            "</if>" +
            "<if test='phone!=null'>" +
            "AND phone = #{phone} " +
            "</if>" +
            "<if test='name!=null'>" +
            "AND `name` LIKE #{name} " +
            "</if>" +
            "GROUP BY time" +
            " </script>")
    List<Orders_Detail> selectOrderList(@Param("startDate") String startDate,@Param("phone") String phone,@Param("name") String name);
}
